var a = getApp();

Page({
    data: {
        id: "",
        baseUrl: a.globalData.baseUrl,
        goods: {},
        address: {},
        num: 1,
        totalPay: 0,
        userId: wx.getStorageSync("userId")
    },
    onLoad: function(t) {
        this.setData({
            id: t.id,
            userId: a.globalData.userId
        });
        var e = a.globalData.urlBase + a.globalData.urlGoodsDetail;
        this.getGoodsData(e);
    },
    getGoodsData: function(a) {
        var t = this;
        wx.request({
            url: a,
            data: {
                goodsId: t.data.id
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var e = JSON.parse(a.data);
                console.log(e), 0 != e.code ? wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 2e3
                }) : t.setData({
                    goods: e.goods,
                    totalPay: e.goods.price
                });
            }
        });
    },
    getAddressData: function(t) {
        var e = this;
        wx.request({
            url: t,
            data: {
                userId: e.data.userId,
                addressId: a.globalData.addressId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var o = JSON.parse(t.data);
                0 != o.code && 99 != o.code ? wx.showToast({
                    title: o.msg,
                    icon: "none",
                    duration: 2e3
                }) : 99 == o.code ? wx.showModal({
                    title: "您尚没有默认收货地址",
                    content: "请先添加收货地址",
                    success: function(a) {
                        a.confirm ? wx.navigateTo({
                            url: "../../pages/delivAddress/delivAddress"
                        }) : a.cancel;
                    }
                }) : (a.globalData.addressId = o.address.id, e.setData({
                    address: o.address
                }));
            }
        });
    },
    mulNum: function() {
        var a = this, t = a.data.num, e = a.data.goods.price;
        t <= 1 ? t = 1 : t -= 1, a.setData({
            num: t,
            totalPay: t * e
        });
    },
    addNum: function() {
        var a = this, t = a.data.num, e = a.data.goods.price;
        t >= 99 ? t = 99 : t += 1, a.setData({
            num: t,
            totalPay: t * e
        });
    },
    checkNum: function(a) {
        var t = this, e = a.detail.value, o = t.data.goods.price;
        e >= 99 ? e = 99 : e <= 1 && (e = 1), t.setData({
            num: e,
            totalPay: e * o
        });
    },
    onReady: function() {},
    onShow: function() {
        var t = a.globalData.urlBase + a.globalData.urlDefaultAddress;
        this.getAddressData(t);
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    seeAddress: function() {
        wx.navigateTo({
            url: "../delivAddress/delivAddress"
        });
    },
    toPay: function() {
        var t = this;
        0 == a.globalData.addressId.length ? wx.navigateTo({
            url: "../../pages/delivAddress/delivAddress"
        }) : t.createOrder();
    },
    createOrder: function() {
        var t = this, e = t.data.id, o = t.data.num, d = a.globalData.addressId, s = a.globalData.urlBase + a.globalData.urlAddGoodsOrder;
        wx.request({
            url: s,
            data: {
                goodsId: e,
                num: o,
                addressId: d,
                userId: t.data.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(e) {
                var o = JSON.parse(e.data);
                0 != o.code ? wx.showToast({
                    title: o.msg,
                    icon: "none",
                    duration: 2e3
                }) : (a.globalData.goodsId = "", a.globalData.addressId = "", t.getPayData(o.msg));
            }
        });
    },
    getPayData: function(t) {
        var e = this, o = a.globalData.urlBase + a.globalData.urlPayData;
        wx.request({
            url: o,
            data: {
                orderId: t,
                userId: e.data.userId
            },
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(a) {
                var t = JSON.parse(a.data);
                0 != t.code ? wx.showToast({
                    title: t.msg,
                    icon: "none",
                    duration: 2e3
                }) : t.price > 0 ? e.wxPay(t) : wx.navigateTo({
                    url: "../order/mallOrder"
                });
            }
        });
    },
    wxPay: function(a) {
        wx.requestPayment({
            timeStamp: a.timeStamp,
            nonceStr: a.nonceStr,
            package: "prepay_id=" + a.totalId,
            signType: "MD5",
            paySign: a.sign,
            success: function(a) {
                wx.navigateTo({
                    url: "../order/mallOrder"
                });
            },
            fail: function(a) {
                wx.showToast({
                    title: "支付失败",
                    icon: "none",
                    duration: 2e3,
                    complete: function() {
                        wx.navigateTo({
                            url: "../order/mallOrder"
                        });
                    }
                });
            },
            complete: function(a) {}
        });
    }
});